package edu.sdjzu.exam.mapper;

import edu.sdjzu.exam.domain.course.TeacherCourse;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface TeacherCourseMapper {
    
    @Insert("INSERT INTO teacher_courses (teacher_id, course_id, permission_type, assigned_by) " +
            "VALUES (#{teacherId}, #{courseId}, #{permissionType}, #{assignedBy})")
    @Options(useGeneratedKeys = true, keyProperty = "teacherCourseId")
    void insert(TeacherCourse teacherCourse);
    
    @Delete("DELETE FROM teacher_courses WHERE teacher_id = #{teacherId} AND course_id = #{courseId}")
    int delete(@Param("teacherId") Long teacherId, @Param("courseId") Long courseId);
    
    @Update("UPDATE teacher_courses SET permission_type = #{permissionType} " +
            "WHERE teacher_id = #{teacherId} AND course_id = #{courseId}")
    int updatePermission(@Param("teacherId") Long teacherId, 
                        @Param("courseId") Long courseId, 
                        @Param("permissionType") String permissionType);
    
    // 检查老师是否有课程权限
    @Select("SELECT permission_type FROM teacher_courses " +
            "WHERE teacher_id = #{teacherId} AND course_id = #{courseId} AND status = 'active'")
    String getTeacherPermission(@Param("teacherId") Long teacherId, @Param("courseId") Long courseId);
    
    // 获取课程的所有授权老师
    @Select("SELECT tc.*, u.username, u.realname FROM teacher_courses tc " +
            "JOIN users u ON tc.teacher_id = u.user_id " +
            "WHERE tc.course_id = #{courseId} AND tc.status = 'active' " +
            "ORDER BY tc.assigned_at")
    @Results({
        @Result(property = "teacherCourseId", column = "teacher_course_id"),
        @Result(property = "teacherId", column = "teacher_id"),
        @Result(property = "courseId", column = "course_id"),
        @Result(property = "permissionType", column = "permission_type"),
        @Result(property = "assignedBy", column = "assigned_by"),
        @Result(property = "assignedAt", column = "assigned_at"),
        @Result(property = "teacher.username", column = "username"),
        @Result(property = "teacher.realname", column = "realname")
    })
    List<TeacherCourse> findTeachersByCourse(@Param("courseId") Long courseId);
    
    // 获取老师的所有课程权限
    @Select("SELECT tc.*, c.course_name, c.course_code FROM teacher_courses tc " +
            "JOIN courses c ON tc.course_id = c.course_id " +
            "WHERE tc.teacher_id = #{teacherId} AND tc.status = 'active' " +
            "ORDER BY c.course_name")
    @Results({
        @Result(property = "teacherCourseId", column = "teacher_course_id"),
        @Result(property = "teacherId", column = "teacher_id"),
        @Result(property = "courseId", column = "course_id"),
        @Result(property = "permissionType", column = "permission_type"),
        @Result(property = "assignedBy", column = "assigned_by"),
        @Result(property = "assignedAt", column = "assigned_at"),
        @Result(property = "course.courseName", column = "course_name"),
        @Result(property = "course.courseCode", column = "course_code")
    })
    List<TeacherCourse> findCoursesByTeacher(@Param("teacherId") Long teacherId);
}